package cn.com.blackboard.DataModule4B2.BBDataUtils;

import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import blackboard.data.ValidationException;
import blackboard.data.course.Course;
import blackboard.data.course.CourseMembership;
import blackboard.data.course.CourseMembership.Role;
import blackboard.data.user.User;
import blackboard.persist.KeyNotFoundException;
import blackboard.persist.PersistenceException;
import blackboard.persist.course.CourseDbLoader;
import blackboard.persist.course.CourseDbPersister;
import blackboard.persist.course.CourseMembershipDbLoader;
import blackboard.persist.course.CourseMembershipDbPersister;
import blackboard.persist.user.UserDbLoader;
import blackboard.persist.user.UserDbPersister;
import cm.com.blackboard.DataModule4B2.customizeConfigUtils.DataSourceKeyUtil;
import cm.com.blackboard.DataModule4B2.customizeConfigUtils.ExtentionAttributeUtil;
import cn.com.blackboard.DataModule4B2.model.StudentCourseInfo;
import cn.com.blackboard.DataModule4B2.model.TeacherCourseInfo;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;

public class CourseMembershipUtil {

private static final Logger logger = LoggerFactory.getLogger(CourseMembershipUtil.class);
	
	private static CourseDbLoader courseLoader = null;
	private static CourseDbPersister coursePersister = null;
	static {
		try {
			courseLoader = CourseDbLoader.Default.getInstance();
			coursePersister = CourseDbPersister.Default.getInstance();
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil init courseLoader/coursePersister"); 
		}
	}
	
	private static UserDbLoader userDbLoader = null;
	private static UserDbPersister userDbPersister = null;
	static {
		try {
			userDbLoader = UserDbLoader.Default.getInstance();
			userDbPersister = UserDbPersister.Default.getInstance();
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil init UserDbLoader/UserDbPersister"); 
		}
	}
	
	private static CourseMembershipDbLoader courseMembershipDbLoader = null;
	private static CourseMembershipDbPersister courseMembershipDbPersister = null;
	static {
		try {
			courseMembershipDbLoader = CourseMembershipDbLoader.Default.getInstance();
			courseMembershipDbPersister = CourseMembershipDbPersister.Default.getInstance();
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil init CourseMembershipDbLoader/CourseMembershipDbPersister"); 
		}
	}
	
	public static Status importTeacherCourseMembership(TeacherCourseInfo teacherCourseInfo){
		User user = null;
		try {
			user = userDbLoader.loadByUserName(teacherCourseInfo.getUserId());
		} catch (KeyNotFoundException e) {
			logger.info("error in CourseMembershipUtil user can't be found");
			return Status.SubRelationFailed;
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil user persistenceException");
			return Status.SubRelationFailed;
		}
		Course course = null;
		try {
			course = courseLoader.loadByCourseId(teacherCourseInfo.getCourseId());
		} catch (KeyNotFoundException e) {
			logger.info("error in CourseMembershipUtil course can't be found");
			return Status.SubRelationFailed;
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil course persistenceException");
			return Status.SubRelationFailed;
		}
		CourseMembership cm = new CourseMembership();
		cm.setCourseId(course.getId());
		cm.setUser(user);
		cm.setUserId(user.getId());
		cm.setEnrollmentDate(Calendar.getInstance());
		cm.setRole(Role.INSTRUCTOR);
		if(ExtentionAttributeUtil.hasExtention(cm, teacherCourseInfo)){
			ExtentionAttributeUtil.setExtentionAttribute(cm, teacherCourseInfo);
		}
		DataSourceKeyUtil.setDataSrouceKey(cm, teacherCourseInfo);
		try {
			courseMembershipDbPersister.persist(cm);
			return Status.ImportSuccess;
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil courseEnrollment(teacher) persistenceException");
			return Status.ImportFailed;
		} catch (ValidationException e) {
			logger.info("error in CourseMembershipUtil courseEnrollment(teacher) invalid");
			return Status.ImportFailed;
		}
	}
	
	public static Status importStudentCourseMembership(StudentCourseInfo studentCourseInfo){;
		User user = null;
		try {
			user = userDbLoader.loadByUserName(studentCourseInfo.getUserId());
		} catch (KeyNotFoundException e) {
			logger.info("error in CourseMembershipUtil user can't be found");
			return Status.SubRelationFailed;
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil user persistenceException");
			return Status.SubRelationFailed;
		}
		Course course = null;
		try {
			course = courseLoader.loadByCourseId(studentCourseInfo.getCourseId());
		} catch (KeyNotFoundException e) {
			logger.info("error in CourseMembershipUtil course can't be found");
			return Status.SubRelationFailed;
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil course persistenceException");
			return Status.SubRelationFailed;
		}
		CourseMembership cm = new CourseMembership();
		cm.setCourseId(course.getId());
		cm.setUser(user);
		cm.setUserId(user.getId());
		cm.setEnrollmentDate(Calendar.getInstance());
		cm.setRole(Role.STUDENT);
		if(ExtentionAttributeUtil.hasExtention(cm, studentCourseInfo)){
			ExtentionAttributeUtil.setExtentionAttribute(cm, studentCourseInfo);
		}
		DataSourceKeyUtil.setDataSrouceKey(cm, studentCourseInfo);
		try {
			courseMembershipDbPersister.persist(cm);
			return Status.ImportSuccess;
		} catch (PersistenceException e) {
			logger.info("error in CourseMembershipUtil courseEnrollment(student) persistenceException");
			return Status.ImportFailed;
		} catch (ValidationException e) {
			logger.info("error in CourseMembershipUtil courseEnrollment(student) invalid");
			return Status.ImportFailed;
		}
	}
	
}
